home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Dev / gcc263-src.lha / gcc-2.6.3 / objc / objc-api.h < prev    next >
C/C++ Source or Header  |  1994-07-11  |  15KB  |  453 lines

  1. /* GNU Objective-C Runtime API.
  2.    Copyright (C) 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  14. License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* As a special exception, if you link this library with files compiled
  21.    with GCC to produce an executable, this does not cause the resulting
  22.    executable to be covered by the GNU General Public License.  This
  23.    exception does not however invalidate any other reasons why the
  24.    executable file might be covered by the GNU General Public License. */
  25.  
  26. #ifndef __objc_api_INCLUDE_GNU
  27. #define __objc_api_INCLUDE_GNU
  28.  
  29. #include "objc/objc.h"
  30. #include "objc/hash.h"
  31. #include <stdio.h>
  32.  
  33. /* For functions which return Method_t */
  34. #define METHOD_NULL    (Method_t)0
  35.                                                 /* Boolean typedefs */
  36. /*
  37. ** Method descriptor returned by introspective Object methods.
  38. ** This is really just the first part of the more complete objc_method
  39. ** structure defined below and used internally by the runtime.
  40. */
  41. struct objc_method_description
  42. {
  43.     SEL name;            /* this is a selector, not a string */
  44.     char *types;        /* type encoding */
  45. };
  46.  
  47. /* Filer types used to describe Ivars and Methods.  */
  48. #define _C_ID       '@'
  49. #define _C_CLASS    '#'
  50. #define _C_SEL      ':'
  51. #define _C_CHR      'c'
  52. #define _C_UCHR     'C'
  53. #define _C_SHT      's'
  54. #define _C_USHT     'S'
  55. #define _C_INT      'i'
  56. #define _C_UINT     'I'
  57. #define _C_LNG      'l'
  58. #define _C_ULNG     'L'
  59. #define _C_FLT      'f'
  60. #define _C_DBL      'd'
  61. #define _C_BFLD     'b'
  62. #define _C_VOID     'v'
  63. #define _C_UNDEF    '?'
  64. #define _C_PTR      '^'
  65. #define _C_CHARPTR  '*'
  66. #define _C_ATOM     '%'
  67. #define _C_ARY_B    '['
  68. #define _C_ARY_E    ']'
  69. #define _C_UNION_B  '('
  70. #define _C_UNION_E  ')'
  71. #define _C_STRUCT_B '{'
  72. #define _C_STRUCT_E '}'
  73.  
  74.  
  75.  
  76. /*
  77. ** Set this variable nonzero to print a line describing each
  78. ** message that is sent.  (this is currently disabled)
  79. */
  80. extern BOOL objc_trace;
  81.  
  82.  
  83. /*
  84. ** Whereas a Module (defined further down) is the root (typically) of a file,
  85. ** a Symtab is the root of the class and category definitions within the
  86. ** module.  
  87. ** 
  88. ** A Symtab contains a variable length array of pointers to classes and
  89. ** categories  defined in the module. 
  90. */
  91. typedef struct objc_symtab {
  92.   unsigned long sel_ref_cnt;                     /* Unknown. */
  93.   SEL        refs;                              /* Unknown. */
  94.   unsigned short cls_def_cnt;                   /* Number of classes compiled
  95.                                                   (defined) in the module. */
  96.   unsigned short cat_def_cnt;                   /* Number of categories 
  97.                                                   compiled (defined) in the 
  98.                                                   module. */
  99.   void      *defs[1];                           /* Variable array of pointers.
  100.                                                   cls_def_cnt of type Class* 
  101.                                                   followed by cat_def_cnt of
  102.                                                   type Category_t. */
  103. } Symtab,   *Symtab_t;
  104.  
  105.  
  106. /*
  107. ** The compiler generates one of these structures for each module that
  108. ** composes the executable (eg main.m).  
  109. ** 
  110. ** This data structure is the root of the definition tree for the module.  
  111. ** 
  112. ** A collect program runs between ld stages and creates a ObjC ctor array. 
  113. ** That array holds a pointer to each module structure of the executable. 
  114. */
  115. typedef struct objc_module {
  116.   unsigned long version;                        /* Compiler revision. */
  117.   unsigned long size;                           /* sizeof(Module). */
  118.   const char* name;                             /* Name of the file where the 
  119.                                                   module was generated.   The 
  120.                                                   name includes the path. */
  121.   Symtab_t    symtab;                           /* Pointer to the Symtab of
  122.                                                   the module.  The Symtab
  123.                                                   holds an array of pointers to 
  124.                                                   the classes and categories 
  125.                                                   defined in the module. */
  126. } Module, *Module_t;
  127.  
  128.  
  129. /*
  130. ** The compiler generates one of these structures for a class that has
  131. ** instance variables defined in its specification. 
  132. */
  133. typedef struct objc_ivar* Ivar_t;
  134. typedef struct objc_ivar_list {
  135.   int   ivar_count;                             /* Number of structures (Ivar) 
  136.                                                   contained in the list.  One
  137.                                                   structure per instance 
  138.                                                   variable defined in the
  139.                                                   class. */
  140.   struct objc_ivar {
  141.     const char* ivar_name;                      /* Name of the instance
  142.                                                   variable as entered in the
  143.                                                   class definition. */
  144.     const char* ivar_type;                      /* Description of the Ivar's
  145.                                                   type.  Useful for 
  146.                                                   debuggers. */
  147.     int        ivar_offset;                    /* Byte offset from the base 
  148.                                                   address of the instance 
  149.                                                   structure to the variable. */
  150.  
  151.   } ivar_list[1];                               /* Variable length 
  152.                                                   structure. */
  153. } IvarList, *IvarList_t;
  154.  
  155.  
  156. /*
  157. ** The compiler generates one (or more) of these structures for a class that
  158. ** has methods defined in its specification. 
  159. ** 
  160. ** The implementation of a class can be broken into separate pieces in a file
  161. ** and categories can break them across modules. To handle this problem is a
  162. ** singly linked list of methods. 
  163. */
  164. typedef struct objc_method Method;
  165. typedef Method* Method_t;
  166. typedef struct objc_method_list {
  167.   struct objc_method_list*  method_next;      /* This variable is used to link 
  168.                                                 a method list to another.  It 
  169.                                                 is a singly linked list. */
  170.   int            method_count;               /* Number of methods defined in 
  171.                                                 this structure. */
  172.   struct objc_method {
  173.     SEL         method_name;                  /* This variable is the method's 
  174.                                                 name.  It is a char*. 
  175.                                                   The unique integer passed to 
  176.                                                 objc_msg_send is a char* too.  
  177.                                                 It is compared against 
  178.                                                 method_name using strcmp. */
  179.     const char* method_types;                 /* Description of the method's
  180.                                                 parameter list.  Useful for
  181.                                                 debuggers. */
  182.     IMP         method_imp;                   /* Address of the method in the 
  183.                                                 executable. */
  184.   } method_list[1];                           /* Variable length 
  185.                                                 structure. */
  186. } MethodList, *MethodList_t;
  187.  
  188. struct objc_protocol_list {
  189.   struct objc_protocol_list *next;
  190.   int count;
  191.   Protocol *list[1];
  192. };
  193.  
  194. /*
  195. ** This is used to assure consistent access to the info field of 
  196. ** classes
  197. */
  198. #ifndef HOST_BITS_PER_LONG
  199. #define HOST_BITS_PER_LONG  (sizeof(long)*8)
  200. #endif 
  201.  
  202. #define __CLS_INFO(cls) ((cls)->info)
  203. #define __CLS_ISINFO(cls, mask) ((__CLS_INFO(cls)&mask)==mask)
  204. #define __CLS_SETINFO(cls, mask) (__CLS_INFO(cls) |= mask)
  205.  
  206. /* The structure is of type MetaClass* */
  207. #define _CLS_META 0x2L
  208. #define CLS_ISMETA(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_META))
  209.  
  210.  
  211. /* The structure is of type Class* */
  212. #define _CLS_CLASS 0x1L
  213. #define CLS_ISCLASS(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_CLASS))
  214.  
  215. /*
  216. ** The class is initialized within the runtime.  This means that 
  217. ** it has had correct super and sublinks assigned
  218. */
  219. #define _CLS_RESOLV 0x8L
  220. #define CLS_ISRESOLV(cls) __CLS_ISINFO(cls, _CLS_RESOLV)
  221. #define CLS_SETRESOLV(cls) __CLS_SETINFO(cls, _CLS_RESOLV)
  222.  
  223. /*
  224. ** The class has been send a +initialize message or a such is not 
  225. ** defined for this class
  226. */
  227. #define _CLS_INITIALIZED 0x04L
  228. #define CLS_ISINITIALIZED(cls) __CLS_ISINFO(cls, _CLS_INITIALIZED)
  229. #define CLS_SETINITIALIZED(cls) __CLS_SETINFO(cls, _CLS_INITIALIZED)
  230.  
  231. /*
  232. ** The class number of this class.  This must be the same for both the 
  233. ** class and it's meta class object
  234. */
  235. #define CLS_GETNUMBER(cls) (__CLS_INFO(cls) >> (HOST_BITS_PER_LONG/2))
  236. #define CLS_SETNUMBER(cls, num) \
  237.   ({ (cls)->info <<= (HOST_BITS_PER_LONG/2); \
  238.      (cls)->info >>= (HOST_BITS_PER_LONG/2); \
  239.      __CLS_SETINFO(cls, (((unsigned long)num) << (HOST_BITS_PER_LONG/2))); })
  240.  
  241. /*
  242. ** The compiler generates one of these structures for each category.  A class
  243. ** may have many categories and contain both instance and factory methods.  
  244. */
  245. typedef struct objc_category {
  246.   const char*   category_name;                /* Name of the category.  Name
  247.                                                 contained in the () of the
  248.                                                 category definition. */
  249.   const char*   class_name;                   /* Name of the class to which
  250.                                                 the category belongs. */
  251.   MethodList_t  instance_methods;             /* Linked list of instance
  252.                                                 methods defined in the 
  253.                                                 category. NULL indicates no
  254.                                                 instance methods defined. */
  255.   MethodList_t  class_methods;                /* Linked list of factory 
  256.                                                 methods defined in the
  257.                                                 category.  NULL indicates no
  258.                                                 class methods defined. */
  259.   struct objc_protocol_list *protocols;          /* List of Protocols 
  260.                              conformed to */
  261. } Category, *Category_t;
  262.  
  263. /*
  264. ** Structure used when a message is send to a class's super class.  The
  265. ** compiler generates one of these structures and passes it to
  266. ** objc_msg_super.
  267. */
  268. typedef struct objc_super {
  269.   id      self;                           /* Id of the object sending
  270.                                                 the message. */
  271.   Class* class;                              /* Object's super class. */
  272. } Super, *Super_t;
  273.  
  274. IMP objc_msg_lookup_super(Super_t super, SEL sel);
  275.  
  276. retval_t objc_msg_sendv(id, SEL, arglist_t);
  277.  
  278.  
  279.  
  280. /*
  281. ** This is a hook which is called by objc_lookup_class and
  282. ** objc_get_class if the runtime is not able to find the class.
  283. ** This may e.g. try to load in the class using dynamic loading.
  284. ** The function is guaranteed to be passed a non-NULL name string.
  285. */
  286. extern Class* (*_objc_lookup_class)(const char *name);
  287.  
  288. extern id (*_objc_object_alloc)(Class* class);
  289.  
  290. extern id (*_objc_object_copy)(id object);
  291.  
  292. extern id (*_objc_object_dispose)(id object);
  293.  
  294. Method_t class_get_class_method(MetaClass* class, SEL aSel);
  295.  
  296. Method_t class_get_instance_method(Class* class, SEL aSel);
  297.  
  298. Class* class_pose_as(Class* impostor, Class* superclass);
  299.  
  300. Class* objc_get_class(const char *name);
  301.  
  302. Class* objc_lookup_class(const char *name);
  303.  
  304. Class* objc_next_class(void **enum_state);
  305.  
  306. const char *sel_get_name(SEL selector);
  307.  
  308. const char *sel_get_type(SEL selector);
  309.  
  310. SEL sel_get_uid(const char *name);
  311.  
  312. SEL sel_get_any_uid(const char *name);
  313.  
  314. SEL sel_get_typed_uid(const char *name, const char*);
  315.  
  316. SEL sel_register_name(const char *name);
  317.  
  318. SEL sel_register_typed_name(const char *name, const char*type);
  319.  
  320.  
  321. BOOL sel_is_mapped (SEL aSel);
  322.  
  323. extern id class_create_instance(Class* class);
  324.  
  325. static inline const char *
  326. class_get_class_name(Class* class)
  327. {
  328.   return CLS_ISCLASS(class)?class->name:((class==Nil)?"Nil":0);
  329. }
  330.  
  331. static inline long
  332. class_get_instance_size(Class* class)
  333. {
  334.   return CLS_ISCLASS(class)?class->instance_size:0;
  335. }
  336.  
  337. static inline MetaClass*
  338. class_get_meta_class(Class* class)
  339. {
  340.   return CLS_ISCLASS(class)?class->class_pointer:Nil;
  341. }
  342.  
  343. static inline Class*
  344. class_get_super_class(Class* class)
  345. {
  346.   return CLS_ISCLASS(class)?class->super_class:Nil;
  347. }
  348.  
  349. static inline int
  350. class_get_version(Class* class)
  351. {
  352.   return CLS_ISCLASS(class)?class->version:-1;
  353. }
  354.  
  355. static inline BOOL
  356. class_is_class(Class* class)
  357. {
  358.   return CLS_ISCLASS(class);
  359. }
  360.  
  361. static inline BOOL
  362. class_is_meta_class(Class* class)
  363. {
  364.   return CLS_ISMETA(class);
  365. }
  366.  
  367.  
  368. static inline void
  369. class_set_version(Class* class, long version)
  370. {
  371.   if (CLS_ISCLASS(class))
  372.     class->version = version;
  373. }
  374.  
  375. static inline IMP
  376. method_get_imp(Method_t method)
  377. {
  378.   return (method!=METHOD_NULL)?method->method_imp:(IMP)0;
  379. }
  380.  
  381. IMP get_imp (Class* class, SEL sel);
  382.  
  383. id object_copy(id object);
  384.  
  385. id object_dispose(id object);
  386.  
  387. static inline Class*
  388. object_get_class(id object)
  389. {
  390.   return ((object!=nil)
  391.       ? (CLS_ISCLASS(object->class_pointer)
  392.          ? object->class_pointer
  393.          : (CLS_ISMETA(object->class_pointer)
  394.         ? (Class*)object
  395.         : Nil))
  396.       : Nil);
  397. }
  398.  
  399. static inline const char *
  400. object_get_class_name(id object)
  401. {
  402.   return ((object!=nil)?(CLS_ISCLASS(object->class_pointer)
  403.                          ?object->class_pointer->name
  404.                          :((Class*)object)->name)
  405.                        :"Nil");
  406. }
  407.  
  408. static inline MetaClass*
  409. object_get_meta_class(id object)
  410. {
  411.   return ((object!=nil)?(CLS_ISCLASS(object->class_pointer)
  412.                          ?object->class_pointer->class_pointer
  413.                          :(CLS_ISMETA(object->class_pointer)
  414.                            ?object->class_pointer
  415.                            :Nil))
  416.                        :Nil);
  417. }
  418.  
  419. static inline Class*
  420. object_get_super_class
  421. (id object)
  422. {
  423.   return ((object!=nil)?(CLS_ISCLASS(object->class_pointer)
  424.                          ?object->class_pointer->super_class
  425.                          :(CLS_ISMETA(object->class_pointer)
  426.                            ?((Class*)object)->super_class
  427.                            :Nil))
  428.                        :Nil);
  429. }
  430.  
  431. static inline BOOL
  432. object_is_class(id object)
  433. {
  434.   return CLS_ISCLASS((Class*)object);
  435. }
  436.  
  437. static inline BOOL
  438. object_is_instance(id object)
  439. {
  440.   return (object!=nil)&&CLS_ISCLASS(object->class_pointer);
  441. }
  442.  
  443. static inline BOOL
  444. object_is_meta_class(id object)
  445. {
  446.   return CLS_ISMETA((Class*)object);
  447. }
  448.  
  449. #endif /* not __objc_api_INCLUDE_GNU */
  450.  
  451.  
  452.  
  453.